package cn.broadmax.boot.modules.aj.controller;


import cn.broadmax.boot.base.SuperController;
import cn.broadmax.boot.base.message.SResult;
import cn.broadmax.boot.modules.aj.bean.ajRecordPageSearch;
import cn.broadmax.boot.modules.aj.entity.Record;
import cn.broadmax.boot.modules.aj.mapper.RecordMapper;
import cn.broadmax.boot.modules.aj.service.IRecordService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import jexx.util.Console;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 案卷记录 前端控制器
 * </p>
 *
 * @author jexx
 * @since 2021-06-01
 */
@Api(tags = "卷宗管理")
@RestController
@RequestMapping("/aj/record")
@RequiredArgsConstructor
public class RecordController extends SuperController {

    @Autowired
    private final IRecordService iRecordService;

    @Autowired
    private final RecordMapper recordMapper;


    @ApiOperation("分页查询案卷类型")
    @GetMapping(value = "")
    public SResult<IPage<Record>> pageTypes(ajRecordPageSearch search){
        LambdaQueryWrapper<Record> wrapper = ajRecordPageSearch.createWrap(search);
        IPage<Record> page = iRecordService.page(getPage(), wrapper);
        return SResult.ok("", page);
    }

    @ApiOperation("查询全部案卷类型")
    @GetMapping(value = "/allType")
    public SResult<List<Record>> pageTypes(){
        List<Record> records = recordMapper.selectList(null);
//        Console.log("allType");
//        Console.log(records);
        return SResult.ok("",records);
    }

    @ApiOperation("查询各类型案卷数目")
    @GetMapping(value = "/eachType")
    public SResult<List<Integer>> getEachType(){
        ArrayList<String> ajList = new ArrayList<>();
        ajList.add("刑侦案卷");
        ajList.add("治安案卷");
        ajList.add("行政案卷");
        ajList.add("工作卷");
        ajList.add("侦察卷");

        ArrayList<Integer> list = new ArrayList<>();

        for (String aj : ajList) {
            list.add(getEachNum(aj));
        }

        return SResult.ok("",list);
    }

//    单个类型案卷（方法抽取）
    public Integer getEachNum(String areaName){
        QueryWrapper wrapper = new QueryWrapper();
        wrapper.like("type",areaName);
        return recordMapper.selectCount(wrapper);
    }

    @ApiOperation("添加")
    @PutMapping(value = "/insert")
    public SResult<Boolean> InsertPage(@RequestBody Record record){
        recordMapper.insert(record);
        return SResult.ok("", Boolean.TRUE);
    }

    @ApiOperation("修改")
    @PutMapping(value = "/update/{id}")
    public SResult<Boolean> updatePage(@PathVariable Integer id, @RequestBody Record record){
//        Record.setId(id);
        iRecordService.updateById(record);
        return SResult.ok("", Boolean.TRUE);
    }

    @ApiOperation("删除")
    @DeleteMapping(value = "/delete/{id}")
    public SResult<Boolean> DeletePage(@PathVariable String id){
        iRecordService.removeById(id);
        return SResult.ok("", Boolean.TRUE);
    }



}
